package com.zhika.zishi.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.zhika.zishi.R;
import com.zhika.zishi.beans.recommend.RecommendStrategyBean;

import java.util.ArrayList;

/**
 * Created by shi-02 on 2015/4/13.
 */
public class StrategyAdapter extends BaseAdapter {

    private Context context;

    private ArrayList<RecommendStrategyBean> strategyList;

    public StrategyAdapter(Context context, ArrayList<RecommendStrategyBean> strategyList) {
        this.context = context;
        this.strategyList = strategyList;
    }

    @Override
    public int getCount() {
        return strategyList.size();
    }

    @Override
    public Object getItem(int position) {
        return strategyList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View view;
        ViewHolder holder;
        if (convertView == null) {
            view = LayoutInflater.from(context).inflate(R.layout.item_1_of_homepage, null);
            view.findViewById(R.id.item_1_hobby_row).setVisibility(View.GONE);
            holder = new ViewHolder();
            holder.strategyText = (TextView) view.findViewById(R.id.item_1_strategy_text);
            holder.contentText = (TextView) view.findViewById(R.id.item_1_content_text);
            holder.pictureImageView = (ImageView) view.findViewById(R.id.item_1_picture_image_view);
            holder.commentNumText = (TextView) view.findViewById(R.id.item_1_comment_text);
            holder.collectNumText = (TextView) view.findViewById(R.id.item_1_collect_text);
            holder.likeNumText = (TextView) view.findViewById(R.id.item_1_like_text);

            view.setTag(holder);
        } else {
            view = convertView;
            holder = (ViewHolder) view.getTag();
            resetViewHolder(holder);
        }
        RecommendStrategyBean strategyBean = strategyList.get(position);

        holder.strategyText.setText(strategyBean.getStrategy());
        holder.contentText.setText(strategyBean.getContentStr());

        /**
         * 现在只是测试
         */
        String pictureUrl = strategyBean.getPictureUrlStr();
        holder.pictureImageView.setImageResource(R.drawable.test_3);

        holder.commentNumText.setText(strategyBean.getCommentNumStr());
        holder.collectNumText.setText(strategyBean.getCollectNumStr());
        holder.likeNumText.setText(strategyBean.getLikeNumStr());

        return view;
    }

    private void resetViewHolder(ViewHolder holder) {
        holder.strategyText.setText(null);
        holder.contentText.setText(null);
        holder.pictureImageView.setImageResource(0);
        holder.commentNumText.setText(null);
        holder.collectNumText.setText(null);
        holder.likeNumText.setText(null);
    }

    private class ViewHolder {
        public TextView strategyText;
        public TextView contentText;
        public ImageView pictureImageView;
        public TextView commentNumText;
        public TextView collectNumText;
        public TextView likeNumText;
    }
}
